const config = require('./config');
// const Sequelize = require('sequelize');
const { Sequelize, Model, DataTypes } = require('sequelize');

// 选项1：分别传入参数
const sequelize = new Sequelize(config.mysql.database, config.mysql.user, config.mysql.password, {
  host: config.mysql.host,
  port: config.mysql.port,
  dialect: 'mysql' 
});

async function connect(req, res) {
    try {
        await sequelize.authenticate();
        console.log('Connection has been established successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
}
connect();

module.exports = sequelize;
// class User extends Model {}
// User.init({
//   username: DataTypes.STRING,
// }, { sequelize, modelName: 'user' });

// sequelize.sync()
//   .then(() => User.create({
//     username: 'janedoe',
//   }))
//   .then(jane => {
//     console.log(jane.toJSON());
//   });

// 选项1：传入连接URI
// const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');
// var mysql = require('mysql');
// const config = require('./config');

// var db = mysql.createConnection({
//     host: config.mysql.host,
//     port: config.mysql.port,
//     user: config.mysql.user,
//     password: config.mysql.password,
//     database: config.mysql.database
// });

// db.connect();

// db.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
//     if (error) throw error;
//     console.log('The solution is: ', results[0].solution);
// });

// var  sql = 'SELECT * FROM device';
// //查
// db.query(sql,function (err, result) {
//         if(err){
//           console.log('[SELECT ERROR] - ',err.message);
//           return;
//         }
 
//        console.log('--------------------------SELECT----------------------------');
//        console.log(result);
//        console.log('------------------------------------------------------------\n\n');  
// });